﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompanyBussiness
{
    public class ServicesBasic:IServicesBasic
    {
        private CompanyDataContext companydata = new CompanyDataContext();

        public virtual List<Intro> GetParentIntro(int languagesID, Boolean introStatus)
        { 
            var intro=(from i in companydata.Intros
                       where i.LanguagesID==languagesID && i.IntroStatus==introStatus && i.ParentIntroID==null
                       select i).ToList();
            return intro;
        }
        public virtual List<Intro> GetChildrenIntroByParentID(int parentIntroID, int languagesID, Boolean introStatus, int StartRow)
        {
            var intro = (from i in companydata.Intros
                         where i.LanguagesID == languagesID && i.IntroStatus == introStatus && i.ParentIntroID == parentIntroID
                         select i).Skip(StartRow).Take(10);
            return intro.ToList();
        }
        public virtual Intro GetIntroByID(int introID)
        {
            var intro = (from i in companydata.Intros
                         where i.IntroID == introID
                         select i).Single();
            return intro;
        }
        public virtual List<Intro> GetIntroBySubject(string introSubject, int languagesID, Boolean introStatus, int StartRow)
        {
            var intro = (from i in companydata.Intros
                         where i.LanguagesID == languagesID && i.IntroStatus == introStatus && i.IntroSubject.Contains(introSubject)==true
                         select i).Skip(StartRow).Take(10);
            return intro.ToList();
        }
        public virtual List<Intro> GetIntroByStatus(int languagesID, Boolean introStatus, int StartRow)
        {
            var intro = (from i in companydata.Intros
                         where i.LanguagesID == languagesID && i.IntroStatus == introStatus
                         select i).Skip(StartRow).Take(10);
            return intro.ToList();
        }

        public virtual void CreateIntro(int parentIntroID, string introSubject, string introContent, string introSource, string introAuthor, Boolean introStatus, int languagesID)
        {
            Intro intro = new Intro();
            intro.ParentIntroID = parentIntroID;
            intro.IntroSubject = introSubject;
            intro.IntroContent = introContent;
            intro.IntroSource = introSource;
            intro.IntroAuthor = introAuthor;
            intro.IntroStatus = introStatus;
            intro.LanguagesID = languagesID;

            companydata.Intros.InsertOnSubmit(intro);
            companydata.SubmitChanges();
        }
        public virtual void UpdateIntro(int introID, int parentIntroID, string introSubject, string introContent, string introSource, string introAuthor, Boolean introStatus, int languagesID)
        {
            var intro = (from i in companydata.Intros
                         where i.IntroID == introID
                         select i).Single();
 
            intro.ParentIntroID = parentIntroID;
            intro.IntroSubject = introSubject;
            intro.IntroContent = introContent;
            intro.IntroSource = introSource;
            intro.IntroAuthor = introAuthor;
            intro.IntroStatus = introStatus;
            intro.LanguagesID = languagesID;

            companydata.SubmitChanges();

        }
        public virtual void DeleteIntro(int introID)
        {
            var intro = (from i in companydata.Intros
                         where i.IntroID == introID
                         select i).Single();

            companydata.Intros.DeleteOnSubmit(intro);
            companydata.SubmitChanges();
        }


        public virtual Contact GetContactByID(int contactID)
        { 
            var contact=(from co in companydata.Contacts
                         where co.ContactID==contactID
                         select co).Single();
            return contact;
        }
        public virtual Contact GetContact(int languagesID, Boolean contactStatus)
        {
            var contact = (from co in companydata.Contacts
                           where co.LanguagesID==languagesID && co.ContactStatus==contactStatus
                           select co).Single();
            return contact;
        }
        public virtual List<Contact> GetAllContact(int languagesID, Boolean contactStatus, int StartRow)
        {
            var contact = (from co in companydata.Contacts
                           where co.LanguagesID == languagesID && co.ContactStatus == contactStatus
                           select co).Skip(StartRow).Take(10).ToList();
            return contact;
        }

        public virtual void CreateContact(string contactName, string contactCell, string contactFax, string contactNick, string contactEmail, string contactCompany, string contactAddress, string contactWebSite, Boolean contactStatus, int languagesID)
        {
            Contact contact = new Contact();
            contact.ContactName = contactName;
            contact.ContactCell = contactCell;
            contact.ContactFax = contactFax;
            contact.ContactNick = contactNick;
            contact.ContactEmail = contactEmail;
            contact.ContactCompany = contactCompany;
            contact.ContactAddress = contactAddress;
            contact.ContactWebSite = contactWebSite;
            contact.ContactStatus = contactStatus;
            contact.LanguagesID = languagesID;

            companydata.Contacts.InsertOnSubmit(contact);
            companydata.SubmitChanges();
        }
        public virtual void UpdateContact(int contactID, string contactName, string contactCell, string contactFax, string contactNick, string contactEmail, string contactCompany, string contactAddress, string contactWebSite, Boolean contactStatus, int languagesID)
        {
            var contact = (from co in companydata.Contacts
                           where co.ContactID == contactID
                           select co).Single();

            contact.ContactName = contactName;
            contact.ContactCell = contactCell;
            contact.ContactFax = contactFax;
            contact.ContactNick = contactNick;
            contact.ContactEmail = contactEmail;
            contact.ContactCompany = contactCompany;
            contact.ContactAddress = contactAddress;
            contact.ContactWebSite = contactWebSite;
            contact.ContactStatus = contactStatus;
            contact.LanguagesID = languagesID;

            companydata.SubmitChanges();
        }
        public virtual void DeleteContact(int contactID)
        {
            var contact = (from co in companydata.Contacts
                           where co.ContactID == contactID
                           select co).Single();

            companydata.Contacts.DeleteOnSubmit(contact);
            companydata.SubmitChanges();
        }

    }
}
